package com.rachel.liveplayer.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.rachel.liveplayer.BaseActivity;
import com.rachel.liveplayer.R;
import com.rachel.liveplayer.fragment.FunFragment;
import com.rachel.liveplayer.fragment.HomeFragment;
import com.rachel.utilslib.utils.StatusUtils;

public class MainActivity extends BaseActivity implements  View.OnClickListener {
    /**
     * const
     */
    private static final String TAG = "zsr";
    private static final int HOME = 1;
    private static final int FUN = 2;
    /**
     * UI
     */
    private TextView mHometext;
    private FrameLayout mHomeLy;
    private ImageView mHomeicon;

    private TextView mFuntext;
    private FrameLayout mFunLy;
    private ImageView mFunicon;

    /**
     * logic
     */
    private Fragment mHomeFragment,mFunFragment;



    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        mHomeicon = (ImageView) findViewById(R.id.home_img);
        mHometext = (TextView) findViewById(R.id.home_text);
        mHomeLy = (FrameLayout) findViewById(R.id.home_ly);
        mHomeLy.setOnClickListener(this);
        mFunicon = (ImageView) findViewById(R.id.fun_img);
        mFuntext = (TextView) findViewById(R.id.fun_text);
        mFunLy = (FrameLayout) findViewById(R.id.fun_ly);
        mFunLy.setOnClickListener(this);
    }

    @Override
    public void initData() {
        StatusUtils.setStatusColor(this, getResources().getColor(R.color.maincolor));




        // 第一次加载的是首页
        mHomeFragment = HomeFragment.newInStance(HomeFragment.TAG);
        getSupportFragmentManager().beginTransaction().add(
                R.id.main_content,
                mHomeFragment,
                mHomeFragment.getTag()).commit();
        selectCurrentPage(HOME);

    }







    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_ly:
                selectCurrentPage(HOME);
                break;
            case R.id.fun_ly:
                selectCurrentPage(FUN);
                break;
            default:
                break;
        }

    }

    /**
     * 切换不同的页面
     * @param index
     */
    private void selectCurrentPage(int index){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        resetBottomSelectd(ft);
        Log.d(TAG, "selectCurrentPage: "+index);
        switch(index){
            case HOME:
                mHomeicon.setImageResource(R.mipmap.icon_home_pager_b);
                mHometext.setTextColor(getResources().getColor(R.color.colorAccent));
                if (mHomeFragment == null){
                    mHomeFragment = HomeFragment.newInStance(HomeFragment.TAG);
                    ft.add(R.id.main_content,mHomeFragment,HomeFragment.TAG);
                }else{
                    ft.show(mHomeFragment);
                }
                break;
            case FUN :
                mFunicon.setImageResource(R.mipmap.icon_fun_pager_b);
                mFuntext.setTextColor(getResources().getColor(R.color.colorAccent));
                if (mFunFragment == null){
                    mFunFragment = FunFragment.newInStance(FunFragment.TAG);
                    ft.add(R.id.main_content,mFunFragment,FunFragment.TAG);
                }else{
                    ft.show(mFunFragment);
                }
                break;
            default :
                break;

        }
        ft.commit();
    }

    /**
     * 先恢复图片和文字的初始状态
     */
    private void resetBottomSelectd(FragmentTransaction ft) {
        mHomeicon.setImageResource(R.mipmap.icon_home_pager);
        mHometext.setTextColor(getResources().getColor(R.color.gray_cc));
        mFunicon.setImageResource(R.mipmap.icon_fun_pager);
        mFuntext.setTextColor(getResources().getColor(R.color.gray_cc));

        if (mHomeFragment != null){
            ft.hide(mHomeFragment);
        }

        if (mFunFragment != null){
            ft.hide(mFunFragment);
        }
    }
}
